<?php

/**
 * informes actions.
 *
 * @package    BecArTe
 * @subpackage informes
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class informesActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        
    }

    public function executeVerInscriptos(sfWebRequest $request) {
        $config = sfTCPDFPluginConfigHandler::loadConfig('informeDeInscriptos');

        // pdf object
        $pdf = new sfTCPDF();

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('BecArTe');
        $pdf->SetTitle('Informes de Inscriptos');
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE . ' 001', PDF_HEADER_STRING);

        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // ---------------------------------------------------------
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('dejavusans', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        $categorias = CategoriaTable::getInstance()->findAll();
        $html = '<h1>Informes de inscriptos</h1>';
        $html = $html . '<ol> ';
        foreach ($categorias as $categoria) {
            $html = $html . '<div><li> <strong> ' . $categoria->getNombre() . ' </strong>';


            $html = $html . '<ol>';

            foreach ($categoria->getBecas() as $beca) {
                $html = $html . '<li>' . $beca->getNombre();

                $html = $html . '<ol>';
                foreach ($beca->getSolicitudes() as $inscripto) {
                    $html = $html . '<li>' . $inscripto->getFecha() . ' -- ' . $inscripto->getEstado() . ' -- ' . $inscripto->getAlumno()->getDocumento() . ' -- ' . $inscripto->getAlumno()->getApellido() . ' ' . $inscripto->getAlumno()->getNombre() . ' </li>';
                }
                $html = $html . '</ol>';
                $html = $html . ' </li></div>';
            }


            $html = $html . '</ol>';
            $html = $html . '</li>';
        }

        $html = $html . '</ol>';

        $pdf->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = '', $autopadding = true);

//        $pdf->writeHTML($html, true, false, true, false, '');
        // ---------------------------------------------------------
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('example_001.pdf', 'I');

        // Stop symfony process
        throw new sfStopException();
    }

    public function executeInformeConsejo(sfWebRequest $request) {
        $this->getContext()->getConfiguration()->loadHelpers('Text');
        $config = sfTCPDFPluginConfigHandler::loadConfig('informeConsejo');

        // pdf object
        $pdf = new sfTCPDF();

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('BecArTe');
        $pdf->SetTitle('Informes Consejo');
//        $pdf->SetSubject('TCPDF Tutorial');
//        $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE . ' ', PDF_HEADER_STRING);

        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // ---------------------------------------------------------
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('dejavusans', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();


        $beca = BecaTable::getInstance()->findOneById($request->getParameter('id'));
        $html = '<h1>Informe Consejo</h1>';

        $html = $html . '<h3>' .
                        $beca->getNombre() .
                        ($beca->getCupo() ? ' (cupo: '.$beca->getCupo().' becas)' : '') .
                        '</h3>';

        $solicitudesEnEvaluacion = $beca->getSolicitudesEnEvaluacion();
        $html = $html . '<div>Hay '.count($solicitudesEnEvaluacion).' solicitudes en evaluación:<ol>';
        foreach ($solicitudesEnEvaluacion as $solicitud) {
          $html .= '<li>' .
                   $solicitud->getAlumno()->getApellido() . ' ' . $solicitud->getAlumno()->getNombre() . ' -- ' . $solicitud->getAlumno()->getTipoDoc().' '. $solicitud->getAlumno()->getDocumento() .
                   '</li>';
        }
        $html = $html . '</ol></div>';

        $pdf->writeHTML($html, true, false, true, false, '');


        foreach ($solicitudesEnEvaluacion as $solicitud) {
           $pdf->AddPage();
           $alu = $solicitud->getAlumno();
           $html = '<p><strong>Aspirante: </strong>' . $alu->getApellido() . ', '. $alu->getNombre() . '</p>';
           $html = $html . '<p><strong>Documento: </strong>' .
                           $alu->getTipoDoc() . ' ' . $alu->getDocumento() . '<br />';
           $html = $html . '<strong>CUIL: </strong>' . $alu->getCuil() . '<br />';
           $html = $html . '<strong>Nacionalidad: </strong>' . $alu->getNacionalidad() . '<br />';
           $html = $html . '<strong>Ciudad de Nacimiento: </strong>' . $alu->getCiudadNacimiento() . '<br />';
           $html = $html . '<strong>Fecha Nacimiento: </strong>' . $alu->getFechaNacimiento() .
                           ' (<strong>edad: </strong>'.$alu->getEdad().' años)<br />';
           $html = $html . '<strong>Domicilio: </strong>' . $alu->getDomicilio() .
                           ', ' . $alu->getCiudadActual() .
                           ', ' . $alu->getCiudadActual()->getProvincia() . '</p>';

           if($solicitud->getObservacion()) {
             $html = $html . '<div><strong>Observaciones de la solicitud: </strong>' .
                           simple_format_text( $solicitud->getObservacion() ). 
                           '</div>';
           }

           try {
               $wsdl_url = sfConfig::get('app_url_ws_externo');
               $client = new SOAPClient($wsdl_url);
               $return = $client->ObtenerAntecedentesPenales($alu->getTipoDoc(), $alu->getDocumento());
               $antecedentes = $return->item;
           } catch (Exception $e) {
               $antecedentes = array(
                       '', 
                       '<strong>Se produjo un error al intentar obtener los antecedentes penales:</strong><br />'.
                         $e->getMessage()
               );
           }


           if (count($antecedentes) > 1) {
               $html = $html . '<div><strong>Antecedentes Penales: </strong>';
               $html = $html . '<ol>';
               foreach ($antecedentes as $idx => $antecedente) {
                   if($idx) {
                      $html = $html . '<li>'. simple_format_text($antecedente) . '</li>';
                   }
               }
               $html = $html . '</ol></div>';
           } else {
               $html = $html . '<div><p><strong>Antecedentes Penales: </strong>No Posee.</p></div>';
           }

           $pdf->writeHTML($html, true, false, true, false, '');

           $html = '<h4>Requisitos Presentados</h4>';
           foreach ($solicitud->getAcreditaciones() as $acreditacion) {
               $html = $html . '<div><label>'. $acreditacion->getRequisito()->getNombre() . ': ' . '</label>';
               if ($acreditacion->getTipoRequisito()->getNombre() == 'Archivo') {
                   $html = $html . '<br /><img src="' . sfConfig::get('app_dir_imagenes') . $acreditacion->getValorAcreditado() . '"   />';
               } else {
                   $html = $html . $acreditacion->getValorAcreditado();
               }
               $html = $html . '</div>';
           }
           $pdf->writeHTML($html, true, false, false, false, '');
        }

//        
        // ---------------------------------------------------------
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('informeConsejo'.$beca->getId().'.pdf', 'I');
        $beca->setGeneroInforme(true);
        $beca->save();
        // Stop symfony process
        throw new sfStopException();
    }

    
    
        public function executeVerBecasOtorgadas(sfWebRequest $request) {
        $config = sfTCPDFPluginConfigHandler::loadConfig('informeBecasOtorgadas');

        // pdf object
        $pdf = new sfTCPDF();

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('BecArTe');
        $pdf->SetTitle('Becas Otorgadas');
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE . ' 001', PDF_HEADER_STRING);

        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // ---------------------------------------------------------
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('dejavusans', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        $becas = BecaTable::getInstance()->findBy('generoInforme', true);
        $html = '<h1>Becas Otorgadas</h1>';
        $html = $html . '<ol> ';
        foreach ($becas as $beca) {
            $html = $html . '<li> <strong> ' . $beca->getNombre() . ' </strong>';
            $html = $html . '<ol>';

            foreach ($beca->getSolicitudes() as $inscripto) {
                if($inscripto->getEstado() == 'Aprobada'){
                    $html = $html . '<li>' . $inscripto->getFecha() . ' -- ' . $inscripto->getEstado() . ' -- ' . $inscripto->getAlumno()->getDocumento() . ' -- ' . $inscripto->getAlumno()->getApellido() . ' ' . $inscripto->getAlumno()->getNombre() . ' </li>';
                
                }
                
            }


            $html = $html . '</ol>';
            $html = $html . '</li>';
        }
//
//        $html = $html . '</ol>';

        $pdf->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = '', $autopadding = true);

//        $pdf->writeHTML($html, true, false, true, false, '');
        // ---------------------------------------------------------
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('example_001.pdf', 'I');

        // Stop symfony process
        throw new sfStopException();
    }
    
}
